package com.realife.eson.common.asserts;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/4/9 15:23
 * @description:
 */
@Slf4j
@SuppressWarnings("all")
public class AssertNP {
    private AssertNP() {
    }


    /**
     * 合法参数断言
     * 入参要求: 参数无关
     *
     * @param responseEnum
     * @param params
     */
    public static boolean legalParams(Object... params) {
        boolean flag = true;
        for (Object param : params) {
            if (null == param) {
                log.info("[**===================> param null **]");
                return false;
            }
            if (param instanceof String) {
                if (StringUtils.isEmpty(param)) {
                    log.info("[**===================> str empty **]");
                    return false;
                }
            }
            if (param instanceof Collection) {
                if (CollectionUtils.isEmpty((Collection<?>) param)) {
                    log.info("[**===================> collection empty **]");
                    return false;
                }
            }
            if (param instanceof Map) {
                if (CollectionUtils.isEmpty(((Map) param))) {
                    log.info("[**===================> map empty **]");
                    return false;
                }
            }
            //...
        }
        return flag;
    }


    public static boolean okArray(Object[] arr) {
        if (null != arr && arr.length > 0) {
            return true;
        }
        return false;
    }


    /**
     * 全真无操作断言
     *
     * @param expressions
     * @return
     */
    public static boolean allTrue(boolean... expressions) {
        boolean flag = true;
        for (boolean expression : expressions) {
            if (!expression) {
                return false;
            }
        }
        return flag;
    }


    /**
     * 至少有一个成立即可
     *
     * @return
     */
    public static boolean atLeastOneTrue(boolean... expressions) {
        boolean flag = false;
        for (boolean expression : expressions) {
            if (expression) {
                return true;
            }
        }
        return flag;
    }


    /**
     * 全假无操作断言
     *
     * @param expressions
     * @return
     */
    public static boolean allFalse(boolean... expressions) {
        boolean flag = true;
        for (boolean expression : expressions) {
            if (expression) {
                return false;
            }
        }
        return flag;
    }


    /**
     * 如果有一个为假
     *
     * @param expressions
     * @return
     */
    public static boolean ifOneFalse(boolean... expressions) {
        boolean flag = false;
        for (boolean expression : expressions) {
            if (!expression) {
                return true;
            }
        }
        return flag;
    }


    /**
     *
     * @param o1
     * @param o2
     * @return
     */
    public static boolean equals(Object o1, Object o2) {
        if (o1.equals(o2)) {
            return true;
        }
        return false;
    }


    /**
     *
     * @param o1
     * @param o2
     * @return
     */
    public static boolean notEquals(Object o1, Object o2) {
        if (o1.equals(o2)) {
            return false;
        }
        return true;
    }


    /**
     *
     * @param checkTar 受检对象
     * @param rangeTar 不符合预期的结果集
     * @return
     */
    public static boolean hasBadResult(Object checkTar, Object... rangeTar) {
        boolean flag = false;
        for (Object o : rangeTar) {
            //逐个处理
            if (checkTar instanceof String) {
                if (o instanceof String) {
                    //string:string
                    flag = ((String) checkTar).equalsIgnoreCase(((String) o));
                } else {
                    //string:object
                    flag = checkTar.equals(o);
                }
            } else {
                //object:object
                flag = checkTar.equals(o);
            }
            //
            if (flag) {
                return flag;
            }
        }
        return false;
    }


    /**
     *
     * @param checkTar 受检对象
     * @param rangeTar 不符合预期的结果集
     * @return
     */
    public static boolean okResult(Object checkTar, Object... rangeTar) {
        boolean flag = true;
        for (Object o : rangeTar) {
            if (checkTar instanceof String) {
                if (o instanceof String) {
                    flag = ((String) checkTar).equalsIgnoreCase(((String) o));
                } else {
                    flag = checkTar.equals(o);
                }
            } else {
                flag = checkTar.equals(o);
            }
            //
            if (flag) {
                return !flag;
            }
        }
        return true;
    }



    /**
     * @param t            本体
     * @param defaultValue 默认给值
     * @param <T>
     * @return
     */
    public static <T> T defaultIfEmpty(T t, T defaultValue) {
        return legalParams(t) ? t : defaultValue;
    }


    /**
     * @param t            本体
     * @param defaultValue 默认给值
     * @param <T>
     * @return
     */
    public static String bodyParam(Object t, String defaultValue) {
        return legalParams(t) ? JSON.toJSONString(t) : defaultValue;
    }




}

